package com.sls.api.util;

import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.log4j.Logger;

/**
 * 读取properties配置文件
 * 
 * @author cx
 * 
 */
public class PropertiesUtil {

	private static final ReentrantLock lock = new ReentrantLock();
	private static Properties prop = null;
	private static Logger log = Logger.getLogger(PropertiesUtil.class);

	/**
	 * 获取配置文件中key对应的值
	 * 
	 * @param key
	 * @return
	 */
	public static String getValue(String key) {
		if (prop == null)
			readProp();
		return prop.getProperty(key);
	}

	/**
	 * 读取配置文件
	 */
	private static void readProp() {
		if (prop == null) {
			try {
				lock.lock();
				if (prop == null) {
					Properties pro = new Properties();
					try {
						pro.load(PropertiesUtil.class.getClassLoader()
								.getResourceAsStream(
										"conf" + File.separator
												+ "common.properties"));
					} catch (IOException e) {
						log.error(e);
					}
					if (!pro.isEmpty())
						prop = pro;
				}
			} finally {
				lock.unlock();
			}
		}
	}

}
